﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.BusinessRule.System;

public partial class masterdata_Supply_Maintain : PageBase
{
    private decimal id;
    private SupplyManager manager = new SupplyManager();
    private OrgainzationManager orgManager = new OrgainzationManager();
    private ChainManager chainManager = new ChainManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request["id"]))
            decimal.TryParse(Request["id"], out id);

        if (!IsPostBack)
        {
            DataBind();
            if (id > 0)
                this.FillForm();


        }

    }


   

    protected void FillForm()
    {
        SUPPLY supply = manager.Load(id);
        USER_CHAINS userchains = new UserManager().LoadChainsByUser(this.MyStateManager.UserID);
        if (supply != null)
        {

            try
            {
                txtName.Text = supply.NAME;
                txtAddress.Text = supply.ADDRESS;
                TxtContact.Text = supply.CONTACT;
                txtTel.Text = supply.TEL;
                cbxValid.Checked = supply.ACTIVE;
                txtMobile.Text = supply.MOBILE;
                txtYLMB1.Text = supply.IDENTITY1;
                txtYLMB2.Text = supply.IDENTITY2;
                txtYLMB3.Text = supply.IDENTITY3;
                txtYLMB4.Text = supply.IDENTITY4;
                txtYLMB5.Text = supply.IDENTITY5;
            }
            catch { }

        }
    }


    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string LogContent="";
        SUPPLY supply;
        if (id > 0)
            supply = manager.Load(id);
        else
        {
            supply = new SUPPLY();
            supply.CODE = Guid.NewGuid().ToString();
            supply.DOMAINID = MyStateManager.DomainID;

        }
        supply.NAME = txtName.Text;
        supply.ADDRESS = txtAddress.Text;
        supply.CONTACT = TxtContact.Text;
        if (!string.IsNullOrEmpty(txtTel.Text))
            supply.TEL = txtTel.Text;
        if (!string.IsNullOrEmpty(txtMobile.Text))
            supply.MOBILE = txtMobile.Text;
        supply.LAST_UPDATE_TIME = Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeDecimal(DateTime.Now);
        supply.LAST_UPDATER = MyStateManager.Name;
        supply.ACTIVE = cbxValid.Checked;

        //5个预留字段
        if (!string.IsNullOrEmpty(txtYLMB1.Text))
            supply.IDENTITY1 = txtYLMB1.Text;
        if (!string.IsNullOrEmpty(txtYLMB2.Text))
            supply.IDENTITY2 = txtYLMB2.Text;
        if (!string.IsNullOrEmpty(txtYLMB3.Text))
            supply.IDENTITY3 = txtYLMB3.Text;
        if (!string.IsNullOrEmpty(txtYLMB4.Text))
            supply.IDENTITY4 = txtYLMB4.Text;
        if (!string.IsNullOrEmpty(txtYLMB5.Text))
            supply.IDENTITY5 = txtYLMB5.Text;

        try
        {
            manager.Save(supply);

            LogContent += "供应商名称：" + supply.NAME;
            LogContent += " 供应商地址：" + supply.ADDRESS;
            LogContent += " 供应商联系人：" + supply.CONTACT;
            LogContent += " 供应商固定电话：" + supply.TEL;
            LogContent += " 供应商手机：" + supply.MOBILE;
            LogContent += " 最后更新人：" + supply.LAST_UPDATER;
            LogContent += " 最后更新时间：" + supply.LAST_UPDATE_TIME;
            LogContent += " 是否有效：" + supply.ACTIVE;
            LogContent += " 预留1：" + supply.IDENTITY1;
            LogContent += " 预留2：" + supply.IDENTITY2;
            LogContent += " 预留3：" + supply.IDENTITY3;
            LogContent += " 预留4：" + supply.IDENTITY4;
            LogContent += " 预留5：" + supply.IDENTITY5;

            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
             Jumpton.SFA.Server.BusinessRule.System.LogType.Info_Change, this.MyStateManager.Name,
             this.MyStateManager.UserID, LogContent);

            //如果置为无效，清空所有与供应商有关customer_chains
            if (!supply.ACTIVE)
                manager.DeleteSupplysInCustomerChain(supply.ID);
            ShowMessage("保存成功", "Supply_List.aspx");
            
        }
        catch (Exception error)
        {
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
             Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
             this.MyStateManager.UserID, "供应商维护 - 异常:" + error.Message);
            ShowMessage(error.Message);
        }

    }

  
}
